# -*- coding: utf-8 -*-
'''
Created on 2013-5-31
处理命令
@author: 不懂d叛逆
'''
import random
import pyccjh, ccjhSkill

class cmdHandle():
	def __init__(self, mainwindow):
		if isinstance(mainwindow, pyccjh.pyccjh):
			self.__ccjh = mainwindow
		# TODO 命令列表map
		self.__cmds = {"5":self.showdoor,
					"4":self.listskill,
					"1":self.showchangjing,
					"2":self.showwupin,
					"3":self.showzhuangtai}
		return

	def AddText(self, string):
		return self.__ccjh.AddText(string)
	def cmd(self, command):
		command = command.rstrip()
		length = len(command)
		if length == 0:
			return True
		if command.startswith("/t "):
			self.__ccjh.TargetT(command[3:])
			return True
		elif command[:1] == "/":
			pos = command.find(" ")
			if(pos == -1):
				ccmd = "%d %s %d" % (self.__ccjh.user.LongID, command[1:], self.__ccjh.Target)
				self.__ccjh.connect.SendCmd(ccmd)
			else:
				ccmd = "%d %s %d %s" % (self.__ccjh.user.LongID, command[1:pos], self.__ccjh.Target, command[pos + 1:])
				self.__ccjh.connect.SendCmd(ccmd)
			return True
		elif command.startswith("!out") or command.startswith("0"):  # 走路功能!outn n为第几个出口，n为负数则从下到上数
			pos = command[1:]
			if command.startswith("!out"):
				pos = command[4:]
			size = len(self.__ccjh.doors)
			try:
				pos = int(pos)
				if size == 0:
					return
				if pos > size:
					pos = size
				elif pos < 0:
					pos = size + pos + 1
			except:
				pos = random.randint(1, size)
			pos = -pos
			ccmd = "%d go 0 %s" % (self.__ccjh.user.LongID, self.__ccjh.doors[pos].get("D"))
			self.__ccjh.connect.SendCmd(ccmd)
			return True
		else:
			pos = command.find(" ")
			_cmd = command
			param = command
			if(pos != -1):
				param = command[pos + 1:]
				_cmd = command[:pos]
			if _cmd in self.__cmds:
				self.__cmds[_cmd](param)
			else:
				self.AddText("未知的命令%s。" % (command))
			return True
		return True
	# 显示出口
	def showdoor(self, command):
		j = 1
		size = len(self.__ccjh.doors)
		for door in reversed(self.__ccjh.doors):
			self.AddText("%d(%d).%s" % (j, j - size - 1, door.get("D")))
			j += 1
		return

	def listskill(self, command):
		self.AddText("技能信息")
		self.AddText("-"*40)
		if len(self.__ccjh.user.skills) > 0:
			for s in self.__ccjh.user.skills.values():
				self.AddText(s.ToString())
			self.AddText("")
			self.AddText("法术信息")
			self.AddText("-"*40)
			count = 0
			for m in self.__ccjh.user.magics.values():
				s = self.__ccjh.user.FindSkill(m.skill)
				if isinstance(s, ccjhSkill.ccjhSkill):
					if s.enabled or len(s.get("s")) > 0:
						self.AddText(m.ToString(s))
						count += 1
			if count == 0:
				self.AddText("你现在没有任何法术！")
		else:
			self.AddText("你现在没有任何技能！")
		return

	def showchangjing(self, command):
		if self.__ccjh.inCombat:
			self.AddText("战斗场景：%s" % self.__ccjh.where.get("N"))
			self.AddText("---------------------------------------")
			self.AddText("战队 0	  ：状态   AP   MP");
			self.AddText("---------------------------------------")
			if self.__ccjh.user.team == 0:
				self.AddText(self.__ccjh.user.CombatString())
			for v in self.__ccjh.mans.items.values():
				if v.team == 0:
					self.AddText(v.CombatString())
			self.AddText("");
			self.AddText("---------------------------------------")
			self.AddText("战队 1	  ：状态   AP   MP")
			self.AddText("---------------------------------------")
			if self.__ccjh.user.team == 1:
				self.AddText(self.__ccjh.user.CombatString())
			for v in self.__ccjh.mans.items.values():
				if v.team == 1:
					self.AddText(v.CombatString())
			self.AddText("")
			self.AddText("---------------------------------------")
			self.AddText("其他：")
			self.AddText("---------------------------------------")
			if self.__ccjh.user.team == -1:
				self.AddText(self.__ccjh.user.CombatString())
			for v in self.__ccjh.mans.items.values():
				if v.team == -1:
					self.AddText(v.CombatString())
			pass
		else:
			self.AddText("场景：%s" % self.__ccjh.where.get("N"))
			self.AddText("-"*40)
			for v in self.__ccjh.mans.items.values():
				self.AddText(v.ToString())
			self.AddText(self.__ccjh.user.ToString())
		self.AddText("")
		return

	def showwupin(self, command):
		try:
			burden = int(self.__ccjh.user.get("u")) * 100.0 / int(self.__ccjh.user.get("v"))
		except:
			burden = 0.0
		self.AddText("%s携带：(%.1f%%)" % (self.__ccjh.user.get("p"), burden))
		self.AddText("-"*40)
		for v in self.__ccjh.user.items.values():
			self.AddText(v.ToString(False))
		self.AddText("")
		return

	def showzhuangtai(self, command):
		_str = "%s %s(%s)" % (self.__ccjh.user.get("t"), self.__ccjh.user.get("p"), self.__ccjh.user.get("o"))
		self.AddText(_str)
		self.AddText("")
		_str = "年龄：%s		等级：%s" % (self.__ccjh.user.get("a"), self.__ccjh.user.get("b"))
		self.AddText(_str)
		self.AddText("")
		_str = "性别：%s	  职业：%s" % (self.__ccjh.user.get("q"), self.__ccjh.user.get("r"))
		self.AddText(_str)
		self.AddText("")
		_str = "你的师父是：%s" % self.__ccjh.user.get("master")
		self.AddText(_str)
		_str = "负重：%s / %s" % (self.__ccjh.user.get("u"), self.__ccjh.user.get("v"))
		self.AddText(_str)
		self.AddText("")
		_str = "HP: %d / %d(%d)" % (self.__ccjh.user.hp, self.__ccjh.user.eff_hp, self.__ccjh.user.max_hp)
		self.AddText(_str)
		_str = "MP: %d / %d(+%d)" % (self.__ccjh.user.mp, self.__ccjh.user.max_mp, self.__ccjh.user.enforce)
		self.AddText(_str)
		self.AddText("")
		self.AddText("-"*40)
		_str = "力量：%s 体质：%s 智慧：%s 敏捷：%s" % (self.__ccjh.user.get("E"), self.__ccjh.user.get("F"), self.__ccjh.user.get("G"), self.__ccjh.user.get("H"))
		self.AddText(_str)
		self.AddText("")
		_str = "攻击：%s(+%s) 防御：%s(+%s)" % (self.__ccjh.user.get("I"), self.__ccjh.user.get("J"), self.__ccjh.user.get("K"), self.__ccjh.user.get("L"))
		self.AddText(_str)
		self.AddText("-"*40)
		self.AddText("")
		_str = "经验：%s / %s(%s)" % (self.__ccjh.user.get("exp"), self.__ccjh.user.get("nex"), self.__ccjh.user.get("sk"))
		self.AddText(_str)
		_str = "声望：%s" % (self.__ccjh.user.get("c"))
		self.AddText(_str)
		self.AddText("")
		_str = "到目前为止共计杀了%s个人(其中%s个玩家)" % (self.__ccjh.user.get("n"), self.__ccjh.user.get("m"))
		self.AddText(_str)
		self.AddText("")
		return
